<?php

namespace app\common\model\mall;

use think\Model;


class Refund extends Model
{


    // 表名
    protected $name = 'litestore_order_refund';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'refund_type_text'
    ];

    const REFUND_STATUS_APPLY = 0;//申请退款

    const REFUND_STATUS_CANCEL = 1;//取消申请

    const REFUND_STATUS_REFUSE = 2;//拒绝退款

    const REFUND_STATUS_WAIT_BUYER = 3;//等待买家发货

    const REFUND_STATUS_WAIT_SELLER = 4;//等待卖家收货

    const REFUND_STATUS_COMPLETE = 5;//已完成

    const REFUND_TYPE_REFUND = 1;//仅退款

    const REFUND_TYPE_REFUND_RETURN = 2;//退货退款

    const IS_DEL_NO = 0;

    const IS_DEL_YES = 1;

    public function goods()
    {
        return $this->belongsTo('addons\\litestore\\model\\Litestoreordergoods', 'order_goods_id', 'id', [], 'LEFT');
    }

    public function log()
    {
        return $this->hasMany('RefundLog', 'refund_id', 'id')->field('id,refund_id,operate_content,createtime');
    }

    /**
     * 创建退款流水号
     */
    public function createRefundNo()
    {
        return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    public function getRefundTypeList()
    {
        return ['1' => __('Refund_type 1'), '2' => __('Refund_type 2')];
    }

    public function getRefundTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_type']) ? $data['refund_type'] : '');
        $list = $this->getRefundTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getRefundMoneyAttr($value)
    {
        return $value ? (float)$value : 0;
    }

    public function getCreatetimeAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) && $value ? date("Y-m-d H:i:s", $value) : '';
    }

    public function getRefundImagesAttr($value)
    {
        if ($value) {
            $images = explode(',', $value);
            foreach ($images as $k => $v) {
                $images[$k] = cdnurl($v, true);
            }

            return $images;
        } else {
            return [];
        }
    }
}
